package wujinye.com.service.impl;

import wujinye.com.dao.DeptDao;
import wujinye.com.dao.UserDao;
import wujinye.com.dao.UserFocusDao;
import wujinye.com.dao.UserInfoDao;
import wujinye.com.model.Dept;
import wujinye.com.model.User;
import wujinye.com.model.UserFocus;
import wujinye.com.model.UserInfo;
import wujinye.com.service.UserService;
import wujinye.com.utils.DateUtil;
import wujinye.com.utils.StrUtil;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {


    private UserDao userDao = new UserDao();
    private UserInfoDao userInfoDao = new UserInfoDao();
    private UserFocusDao userFocusDao = new UserFocusDao();
    private DeptDao deptDao = new DeptDao();

    @Override
    public User findUserByName(String username) {
        return userDao.findUserByName(username);
    }

    @Override
    public User findUserBaseInfo(String username) {
        User user = userDao.findUserByName(username);
        if (null != user) {
            user.setPassword(null);
        }
        return user;
    }

    @Override
    public User findUserByEmail(String email) {
        return userDao.findUserByEmail(email);
    }

    @Override
    public void addUser(String name, String pwd, String email) {
        User user = new User();
        user.setId(null);
        user.setDeptId(null);
        user.setEmail(email);
        user.setPassword(pwd);
        user.setUsername(name);
        user.setIsSecret("0");
        userDao.addUser(user);
        User userByName = userDao.findUserByName(name);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userByName.getId());
        userInfo.setLook(0);
        userInfo.setRegisterTime(new Date());
        userInfoDao.addUserInfo(userInfo);
    }


    @Override
    public void resetPwd(Integer id, String password) {
        userDao.resetPassword(id, password);
    }

    @Override
    public void updateLoginTime(Integer id) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(id);
        userInfo.setLoginTime(new Date());
        userInfoDao.updateLoginTime(userInfo);
    }

    @Override
    public Map<String, Object> findUserById(Integer id) {
        return userDao.findUserBaseInfoById(id);
    }

    @Override
    public Map<String, Object> findUserDetail(Integer id) {
        Map<String,Object> userDetail =  userInfoDao.getUserDetail(id);
        if (userDetail!=null){
            // 把数据库中datetime类型转换成String类型，并且格式化日期类型（yyyy-MM-dd HH:mm:ss）
            Timestamp registerTime = (Timestamp) userDetail.get("registerTime");
            Timestamp loginTime = (Timestamp) userDetail.get("loginTime");
            userDetail.put("registerTime", DateUtil.timeToString(registerTime));
            userDetail.put("loginTime", DateUtil.timeToString(loginTime));
        }
        return userDetail;
    }

    @Override
    public void updatePic(Integer id, String picPath) {
        userInfoDao.updatePic(id, picPath);
    }

    @Override
    public void updateSecret(User user) {
        Object[] obj;
        if (user.getDeptId() == null) {
            obj = new Object[2];
            obj[0] = user.getIsSecret();
            obj[1] = user.getId();
        } else {
            obj = new Object[3];
            obj[0] = user.getIsSecret();
            obj[1] = user.getDeptId();
            obj[2] = user.getId();
        }
        userDao.updateSecret(obj);
    }

    @Override
    public void updateUserInfo(UserInfo userInfo) {
        Object[] obj = {userInfo.getRealName(), userInfo.getAge(), userInfo.getGender(), userInfo.getPhone(), userInfo.getUserId()};
        userInfoDao.updateUserInfo(obj);
    }

    @Override
    public List<Map<String, Object>> findUserList(Integer uId, String search, int page) {
        List<Map<String, Object>> list = userInfoDao.findUserList(uId, search, page);
        for (Map<String, Object> map : list) {
            if (!StrUtil.checkString(map.get("realName").toString())) {
                map.put("realName", map.get("username"));
            }
        }
        return list;
    }

    @Override
    public long getTotalCount(String search) {
        return userInfoDao.totalUser(search);
    }

    @Override
    public void lookCount(Integer id, Integer times) {
        userInfoDao.addLookCount(id, times);
    }

    @Override
    public UserFocus findFocusBean(Integer uId, Integer ufId) {

        return userFocusDao.findFocusBean(uId, ufId);
    }

    @Override
    public void addFocus(Integer userId, Integer ufId) {
        userFocusDao.addFocus(userId, ufId);
    }

    @Override
    public void removeFocus(Integer userId, Integer ufId) {
        userFocusDao.removeFocus(userId, ufId);
    }

    @Override
    public List<Map<String, Object>> findFocusList(Integer userId, Integer page) {
        List<Map<String, Object>> list = userFocusDao.findFocusList(userId, page);
        if (list != null && list.size() != 0) {
            for (Map<String, Object> map : list) {
                if (!StrUtil.checkString(map.get("realName").toString())) {
                    map.put("realName", map.get("username"));
                }
            }
        }
        return list;
    }

    @Override
    public long getFocusTotalCount(Integer userId) {
        return userFocusDao.getFocusTotalCount(userId);
    }

    @Override
    public List<Map<String,Object>> getUserCount() {
        List<Map<String,Object>> list = userDao.getUserCount();
        List<Dept> depts = deptDao.findAll();
        int count = 0;
        for (Map<String, Object> map :list) {
            map.put("deptName",depts.get(count).getName());
            count++;
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> findUserDeptList() {
        List<Map<String,Object>> list = userInfoDao.findUserDeptList();
        if (null != list){
            for (Map<String, Object> map :list) {
                String realName = map.get("realName").toString();
                if (!StrUtil.checkString(realName)){
                    map.put("realName",map.get("username"));
                }
            }
        }
        return list;
    }


}
